﻿package net.pezzato.genetree.graphics {
	/**
	 * ...
	 * @author Alessandro Pezzato
	 */
	import flash.events.Event;
	import net.pezzato.graphics.Widget;
	import net.pezzato.genetree.info.GeneNode;
	import flash.geom.Rectangle;
	public class Container extends Widget {
		private var nws_:/*NodeWidget*/Array;
		private var zoom_:Number;
		private var dragged_:Boolean;
		private var timeToStartDrag_:Number;
		private var timeDecr_:Number;
		public function Container(wp:Widget) {
			super(wp, "Container");
			nws_ = new Array();
			zoom_ = 1;
			timeToStartDrag_ = 2;
			timeDecr_ = 0;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		public function addNode(node:GeneNode, onw:NodeWidget = null):NodeWidget {
			var nw:FullNodeWidget = new FullNodeWidget(this, node);
			nw.depth = onw.depth + 1;
			var scaleFactor:Number = Math.pow(2, nw.depth);
			nw.width = NodeWidget.COMP_RECT.width / scaleFactor;
			nw.height = NodeWidget.COMP_RECT.height / scaleFactor;
			nw.x = onw.x + (NodeWidget.COMP_RECT.width + NodeWidget.X_MARGIN) / scaleFactor * 2;
			if (node.sex == GeneNode.MALE) {
				nw.y = onw.y - (NodeWidget.Y_MARGIN) / scaleFactor + NodeWidget.COMP_RECT.height / scaleFactor;
			}
			if (node.sex == GeneNode.FEMALE) {
				nw.y = onw.y + (NodeWidget.Y_MARGIN) / scaleFactor;// + NodeWidget.COMP_RECT.height / scaleFactor;
			}
			nws_.push(nw);
			nw.addParents();
			return nw;
		}
		public function drawLink(myl:Link, thl:Link):void {
			var scaleFactor:Number = Math.pow(2, myl.nw.depth);
			graphics.lineStyle(4 / scaleFactor, 0xffffff, 0.75);
			graphics.moveTo(myl.nw.x + myl.pos.x /  scaleFactor, myl.nw.y + myl.pos.y /  scaleFactor);
			graphics.lineTo(thl.nw.x,  thl.nw.y + NodeWidget.COMP_RECT.height / scaleFactor / 4);
		}
		override public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void {
			timeDecr_ = 1;
		}
		override public function stopDrag():void {
			super.stopDrag();
			dragged_ = false;
			timeDecr_ = 0;
			timeToStartDrag_ = 2;
		}
		public function get dragged():Boolean {
			return dragged_;
		}
		private function onEnterFrame(event:Event):void {
			if (timeToStartDrag_ < 0) {
				super.startDrag();
				dragged_ = true;
			} else {
				timeToStartDrag_ -= timeDecr_;
			}
		}
	}
}